/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { HvigorLogger } from '@ohos/hvigor';

export class Logger {
  /**
   * Record error logs
   * @param {string} module - The module name
   * @param {string} msg - The log message
   * @param {...unknown[]} args - Additional parameters
   */
  static error(module: string, msg: string, ...args: unknown[]): void {
    const prefix = `[HMRouterPlugin][${module}]:`;
    HvigorLogger.getLogger().error(`${prefix} ${msg}`, ...args);
  }

  /**
   * Record information logs
   * @param {string} module - The module name
   * @param {string} msg - The log message
   * @param {...unknown[]} args - Additional parameters
   */
  static info(module: string, msg: string, ...args: unknown[]): void {
    const prefix = `[HMRouterPlugin][${module}]:`;
    HvigorLogger.getLogger().info(`${prefix} ${msg}`, ...args);
  }

  /**
   * Record warning logs
   * @param {string} module - The module name
   * @param {string} msg - The log message
   * @param {...unknown[]} args - Additional parameters
   */
  static warn(module: string, msg: string, ...args: unknown[]): void {
    const prefix = `[HMRouterPlugin][${module}]:`;
    HvigorLogger.getLogger().warn(`${prefix} ${msg}`, ...args);
  }

  /**
   * Record debug logs
   * @param {string} module - The module name
   * @param {string} msg - The log message
   * @param {...unknown[]} args - Additional parameters
   */
  static debug(module: string, msg: string, ...args: unknown[]): void {
    const prefix = `[HMRouterPlugin][${module}]:`;
    HvigorLogger.getLogger().debug(`${prefix} ${msg}`, ...args);
  }

  /**
   * Get a logger with context
   * @param {string} module - The module name
   * @param {string} operation - The operation name
   */
  static withContext(module: string, operation: string) {
    return {
      error: (msg: string, ...args: unknown[]) => Logger.error(module, `${operation} - ${msg}`, ...args),
      info: (msg: string, ...args: unknown[]) => Logger.info(module, `${operation} - ${msg}`, ...args),
      warn: (msg: string, ...args: unknown[]) => Logger.warn(module, `${operation} - ${msg}`, ...args),
      debug: (msg: string, ...args: unknown[]) => Logger.debug(module, `${operation} - ${msg}`, ...args),
    };
  }
}
